﻿using DataManage;

namespace CANCommunication
{
    public static class StructTransformation
    {
        #region 结构体转上位机类
        public static DataFrames ToDataFrames(this tagZCAN_Receive_Data tagZLG_CHANNEL_INIT_CONFIG)
        {
            return new DataFrames()
            {
                CANType = ZLGCANProtocolType.CAN,
                DateTimeStamp = tagZLG_CHANNEL_INIT_CONFIG.timestamp,
                CANId = tagZLG_CHANNEL_INIT_CONFIG.frame.can_id & Collection.CAN_ID_FLAG,
                Length = tagZLG_CHANNEL_INIT_CONFIG.frame.can_dlc,
                Data = tagZLG_CHANNEL_INIT_CONFIG.frame.data
            };
        }

        public static DataFrames ToDataFrames(this tagZCAN_ReceiveFD_Data tagZCAN_ReceiveFD_Data)
        {
            return new DataFrames()
            {
                CANType = ZLGCANProtocolType.CANFD,
                DateTimeStamp = tagZCAN_ReceiveFD_Data.timestamp,
                CANId = tagZCAN_ReceiveFD_Data.frame.can_id,
                Length = tagZCAN_ReceiveFD_Data.frame.len,
                Data = tagZCAN_ReceiveFD_Data.frame.data,
                Flag = tagZCAN_ReceiveFD_Data.frame.flags
            };
        }

        #endregion

        #region 上位机类转结构体
        public static tagZCAN_Transmit_Data ToZCAN_Trabsmit_Data(this DataFrames dataFrames)
        {
            return new tagZCAN_Transmit_Data()
            {
                transmit_type = (uint)ZLGSendType.Normal,
                frame = new can_frame()
                {
                    can_id = dataFrames.CANId,
                    can_dlc = dataFrames.Length,
                    data = dataFrames.Data
                },
            };
        }

        public static tagZCAN_TransmitFD_Data ToZCAN_TrabsmitFD_Data(this DataFrames dataFrames)
        {
            return new tagZCAN_TransmitFD_Data()
            {
                transmit_type = (uint)ZLGSendType.Normal,
                frame = new canfd_frame()
                {
                    can_id = dataFrames.CANId,
                    len = dataFrames.Length,
                    data = dataFrames.Data,
                }
            };
        }
        #endregion
    }
}
